package com.bookcity.front.webfront.config;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;

/**
 * @author oldliu
 * @since 1.0
 */
@Component
@Order(1)
//@WebFilter(urlPatterns = "/*")
public class RoleFilter implements Filter {

    @Autowired
    private MyStore redistemplate;

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {

    }

    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException,
            ServletException {
        HttpServletRequest req = (HttpServletRequest) request;
        if (req.getServletPath().equals("/mem/login")) {
            chain.doFilter(request, response);
            return;
        }
        String token = req.getHeader("token");
        if(redistemplate.get(token)==null){
            response.setContentType("application/json;charset=utf-8");
            response.getWriter().print("{'code':401,'msg':'没有登录'}");
            return;
        }
        chain.doFilter(request, response);
    }

    @Override
    public void destroy() {

    }
}
